#include ".\check_string.h"
#include "..\..\bsp\uart\uart.h"

def_simple_fsm(check_string,
               def_params(
                   uint8_t *pchOffest;
                   uint8_t *pchString;
                   fetch_byte_fn *fnGetByte;
               ))

fsm_initialiser(check_string,
                args(
                    uint8_t *pchString,
                    fetch_byte_fn *fnGetByte
                ))
init_body(
    this.pchString = pchString;
    this.fnGetByte = fnGetByte;
)

fsm_implementation(check_string, args(bool *pbIsRquestDrop))
{
    def_states(
        CHECK_ZERO,
        RECV_CHAR,
        COMPARE_CHAR,
    )

    uint8_t chByte;

    *pbIsRquestDrop = false;

    body_begin();

        on_start(
                //*pbIsRquestDrop = false;
                this.pchOffest = this.pchString;
                //LOG_G("check_string:START drop=false\n");
                update_state_to(CHECK_ZERO);
            )

        state(CHECK_ZERO) {
            if (0 == (*this.pchOffest)) {
                // LOG_G("check_string:CHECK_ZERO drop=true check %s cpl\n", this.pchString);
                *pbIsRquestDrop = true;
                fsm_cpl();
            } else {
                // *pbIsRquestDrop = false;
                update_state_to(RECV_CHAR);
            }
        }

        state(RECV_CHAR) {
            if (this.fnGetByte(&chByte)) {
                // *pbIsRquestDrop = false;
                update_state_to(COMPARE_CHAR)
            } else {
                // LOG_G("check_string:RECV_CHAR  drop=false reset fsm\n");
                // *pbIsRquestDrop = false;                
                reset_fsm();
                fsm_on_going();
            }
        }

        state(COMPARE_CHAR) {
            if (*this.pchOffest == chByte) {
                this.pchOffest++;
                // *pbIsRquestDrop = false;
                update_state_to(CHECK_ZERO);
            } else {
                // LOG_G("check_string:COMPARE_CHAR drop=true %c!=%c\n", *this.pchOffest, chByte);
                *pbIsRquestDrop = true;
                reset_fsm();
                fsm_on_going();
            }
        }

    body_end()
}
